

export function stringify(obj, options) {
	const encoder = encodeURIComponent
	const pairs = []
	let i, key, value
   
	if (Array.isArray(obj)) {
	  for (i = 0; i < obj.length; i++) {
		if (typeof obj[i] === 'undefined') {
		  pairs.push(encode(i, obj[i], encoder))
		}
	  }
	} else {
	  for (key in obj) {
		if (Object.prototype.hasOwnProperty.call(obj, key)) {
		  value = obj[key]
		  if (Array.isArray(value)) {
			for (i = 0; i < value.length; i++) {
			  pairs.push(encode(key, value[i], encoder))
			}
		  } else if (typeof value !== 'undefined') {
			pairs.push(encode(key, value, encoder))
		  }
		}
	  }
	}
   
	return pairs.join('&')
  }
   
  export function encode(key, value, encoder) {
	if (typeof value === 'object') {
	  return serializeValue(encoder, key, value)
	}
	return encoder(key) + '=' + encoder(value)
  }
   
  export function serializeValue(encoder, key, value) {
	if (Array.isArray(value)) {
	  return value.map(v => serializeValue(encoder, `${key}[]`, v)).join('&')
	}
   
	if (typeof value === 'object') {
	  return Object.keys(value).map(k => serializeValue(encoder, `${key}[${k}]`, value[k])).join('&')
	}
   
	return encoder(key) + '=' + encoder(value)
  }